package com.android.JiYclock;

import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;

public class Recording extends Activity {

	   private  final static String TAG = "JIY";
	   private Button button_pause = null;
	   private Button button_complete = null;
	   private MediaRecorder recorder = null;
	   private Chronometer cher1 = null;
	   private String path = null;
	   Uri uri = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
	//	setContentView(R.layout.recording;
		path = this.CreatPath();
		// If no data was given in the intent (because we were started
        // as a MAIN activity), then use our default content provider.
	    //cher1 = (Chronometer) this.findViewById(R.id.cher1);
	    cher1.setFormat(null);
		initializeAudio();
		//button_pause = (Button) this.findViewById(R.id.button_pause);  
    	//button_complete = (Button) this.findViewById(R.id.button_complete);  
    	
    	button_complete.setOnClickListener(new AudioListerner());  
    	button_pause.setOnClickListener(new AudioListerner());  
	}  
	
	/**
	 * 
	 */
    private void initializeAudio() {  
        recorder = new MediaRecorder();// 
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
        recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);  
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
        String filename=CreatPath();
        recorder.setOutputFile(filename);  
        try {  
            recorder.prepare();
            recorder.start();
            cher1.start();
            Toast.makeText(Recording.this, "开始录音ʼ", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    class AudioListerner implements OnClickListener {  
        @Override  
        public void onClick(View v) {  
            if (v == button_complete) {  
            	recorder.stop();// 
                // recorder.reset(); //  
                recorder.release(); // 
                Intent intent = new Intent(Recording.this,SetAlarm.class);     
                intent.putExtra("path", path);
                cher1.stop();
                startActivity(intent);
            }  
            if (v == button_pause) {  
                recorder.reset(); // 
                // recorder = null;  
                Toast.makeText(Recording.this, "已运行", Toast.LENGTH_LONG).show();
            }  
        }  
    } 
    
    /**
     * 
     * @return
     */
	private String CreatPath()
	{
		File sd=Environment.getExternalStorageDirectory(); 
        String foder=sd.getPath()+"/AlarmClock"; 
        File file=new File(foder); 
        if(!file.exists()) 
         file.mkdir();
        String name =String.valueOf(System.currentTimeMillis());
        String location =foder+"/"+name+".arm";
		return location; 

	}
}
